package fm.service.impl;

import com.alibaba.fastjson.JSON;
import fm.dao.HibernateBaseDao;
import fm.entity.WxUser;
import fm.service.UserService;
import fm.util.CommonUtils;
import fm.util.Constant;
import fm.util.WechatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 类名：fm.service.impl.UserServiceImpl
 * 创建者： CM .
 * 创建时间：2016/3/7
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    private final static Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    HibernateBaseDao baseDao;

    public long saveOrUpdate(WxUser user) {
        WxUser src = getByOpenIdAndPublicNumberId(user.getOpenid(), user.getPublicNumberId());
        if (CommonUtils.isEmpty(src)) {
            LOGGER.info("add user : {}  ", JSON.toJSON(user));
            baseDao.save(user);
        } else {
            LOGGER.info("update user : {} ,src :{}", JSON.toJSON(user), JSON.toJSONString(src));
            if (!CommonUtils.isEmpty(user.getSubscribed())) {
                src.setSubscribed(user.getSubscribed());
            }
            if (!CommonUtils.isEmpty(user.getRemark())) {
                src.setRemark(user.getRemark());
            }
            if (!CommonUtils.isEmpty(user.getCity())) {
                src.setCity(user.getCity());
            }
            if (!CommonUtils.isEmpty(user.getCountry())) {
                src.setCountry(user.getCountry());
            }
            if (!CommonUtils.isEmpty(user.getNickname())) {
                src.setNickname(user.getNickname());
            }
            if (!CommonUtils.isEmpty(user.getProvince())) {
                src.setProvince(user.getProvince());
            }
            if (!CommonUtils.isEmpty(user.getPrivilege())) {
                src.setPrivilege(user.getPrivilege());
            }
            if (!CommonUtils.isEmpty(user.getTelephone())) {
                src.setTelephone(user.getTelephone());
            }
            baseDao.update(src);
        }
        return user.getId();
    }


    public WxUser getByOpenIdAndUuid(String openId, String uuid) {
        String hql = "select user from WxUser user,PublicNumber pn where user.openid=? and user.publicNumberId=pn.id and pn.uuid=?";
        return (WxUser) baseDao.getUnique(hql, openId, uuid);
    }

    public WxUser getByOpenIdAndPublicNumberId(String openId, Long publicNumberId) {
        String hql = "from WxUser user where user.openid=? and user.publicNumberId=?";
        return (WxUser) baseDao.getUnique(hql, openId, publicNumberId);
    }


    public WxUser getByAuth2(String openId, String appId, String appSecret, HttpServletRequest request) throws Exception {
        String token = WechatUtils.getBaseAccessToken(appId, appSecret, Constant.GRANT_TYPE_CLIENT_CREDENTIAL, request);
        WxUser user = WechatUtils.getUserInformationFromUnion(token, openId, Constant.LANGUAGE_LANG_TYPE_ZH_CN);
        return user;
    }

    @Override
    public WxUser getById(Long userId) {
        return (WxUser) baseDao.getById(WxUser.class, userId);
    }
}
